import Vue from "vue";
import Router from "vue-router";
import dongRouter from "./dongRouter";
import store from "@/store";
Vue.use(Router);
var router = new Router({
  routes: [
    {
      path: "/",
      name: "index",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "Login",
      component: () => import(/* webpackChunkName: "about" */ "@/components/Login"),
    },
    {
      path: "/home",
      name: "home",
      component: () => import(/* webpackChunkName: "about" */ "@/components/Home"),
      children:[]
    },

  ],
});

router.$addRoutes = (params) => {
  router.matcher = new Router({ mode: "hash" }).matcher;
  router.addRoutes(params);
};
// 路由前置守卫，权限判断
// 判断对应路由是否登录，保证有些组件的显示，必须是登录状态
// console.log(process.env.NODE_ENV)
router.beforeEach((to, from, next) => {
  // console.log(store.getters.getMenuInfo);
  // console.log(to.path,from.path);
  if (to.path == "/login") {
    next();
  } else if (store.getters.getToken) {//是否登录
    if (JSON.parse(sessionStorage.getItem('IsRefresh'))){
      // console.log(from.path);
      // console.log(to.path);
      next();
    }else{
      // console.log(JSON.parse(sessionStorage.getItem('IsRefresh')));
      // console.log('重新加载路由');
      dongRouter.DynamicAddRouter();
      sessionStorage.setItem('IsRefresh',true);
      // console.log(to);
      next(to)
    }
  } else {
    console.log('cdx');
    router.replace("/login").catch(err => err);
  }
});
export default router;
